package com.ncmmall.mall.controller.business.distribution;

import com.ncmmall.application.business.member.MemberApplication;
import com.feijin.commons.lang.ArithUtils;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.distribution.DistributionRateRecord;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 商家保低佣金管理
 *
 * @author likaikai
 * @date 2020-8-22 15:51:23
 */
@Controller
@RequestMapping("/distributor")
@Slf4j
public class DistributorController {

    @Autowired
    private MemberApplication memberApplication;

    @Autowired
    private QueryChannelService queryChannelService;

    @RequestMapping(value = "/modifyGuaranteeScale", method = RequestMethod.GET)
    public String modifyGuaranteeScale(Model model) {
        WebUser user = Securitys.currentUser();
        model.addAttribute("scale", user.getGuaranteeScale() == null ? null : (int) ArithUtils.mul(user.getGuaranteeScale(), 100));
        return "/merchant/distribution/distributor/form";
    }

    /**
     * 修改分销商保底佣金比例
     */
    @RequestMapping(value = "/modifyGuaranteeScale", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> modifyGuaranteeScale(int scale) {
        try {
            WebUser user = Securitys.currentUser();
            Requires.isTrue(scale >= 0 && scale <= 100, "保底佣金比例不正确");
            memberApplication.modifyGuaranteeScale(user, ArithUtils.div(scale, 100, 2));
            return ImmutableMap.of("status", true, "message", "保存成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[设置分销保底佣金比例异常]", e);
            return ImmutableMap.of("status", false, "message", "保存异常");
        }
    }

    /**
     * 修改分销商保底佣金比例修改记录
     */
    @RequestMapping(value = "/record", method = RequestMethod.GET)
    public String record(Model model, PageParams pageParams) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo()).setPageSize(pageParams.getPageSize());

        QueryPage<DistributionRateRecord> page = queryChannelService.queryPageEntity("select * from distributor_rate_rerored where merchant_id = :mid order by createTime desc", pageRequest.setParams(ImmutableMap.of("mid", Securitys.currentUser().getId())), DistributionRateRecord.class);
        model.addAttribute("page", page);
        return "/merchant/distribution/distributor/record";
    }

}
